package com.ruoyi.order.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.MyUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.customer.domain.CustomerFinance;
import com.ruoyi.customer.domain.CustomerUser;
import com.ruoyi.customer.service.ICustomerFinanceService;
import com.ruoyi.customer.service.ICustomerStaticService;
import com.ruoyi.customer.service.ICustomerUserService;
import com.ruoyi.market.domain.Wallet;
import com.ruoyi.market.service.IWalletService;
import com.ruoyi.order.domain.OrderWallet;
import com.ruoyi.order.mapper.OrderWalletMapper;
import com.ruoyi.order.service.IOrderWalletService;
import com.ruoyi.thirdpart.service.IBizWechatPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;
import java.math.BigDecimal;
import java.util.List;

/**
 * 充值订单 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class OrderWalletServiceImpl implements IOrderWalletService
{
    @Autowired
    private OrderWalletMapper orderWalletMapper;

    @Autowired
    private ICustomerUserService customerUserService;

    @Autowired
    private ICustomerStaticService customerStaticService;

    @Autowired
    private ICustomerFinanceService customerFinanceService;

    @Autowired
    private IWalletService walletService;

    @Autowired
    private IBizWechatPayService bizWechatPayService;

    @Autowired
    protected Validator validator;

    /**
     * 根据条件分页查询充值订单列表
     * 
     * @param orderWallet 充值订单信息
     * @return 充值订单信息集合信息
     */
    @Override
    public List<OrderWallet> selectOrderWalletList(OrderWallet orderWallet)
    {
        return orderWalletMapper.selectOrderWalletList(orderWallet);
    }

    /**
     * 通过充值订单编号查询充值订单
     * 
     * @param orderNo 充值订单编号
     * @return 充值订单对象信息
     */
    @Override
    public OrderWallet selectOrderWalletByOrderNo(String orderNo)
    {
        return orderWalletMapper.selectOrderWalletByOrderNo(orderNo);
    }

    /**
     * 新增保存充值订单信息
     * 
     * @param orderWallet 充值订单信息
     * @return 结果
     */
    @Override
    @Transactional
    public OrderWallet insertOrderWallet(OrderWallet orderWallet)
    {
        // 新增充值订单信息
        Long userId = SecurityUtils.getUserId();
        String orderNo = MyUtils.getOrderNoByTimeAndId(userId);

        Wallet wallet = walletService.selectWalletById(orderWallet.getWalletId());

        CustomerUser customerUser = customerUserService.selectUserById(userId);

        orderWallet.setShopId(customerUser.getShopId());
        orderWallet.setAppId(customerUser.getAppId());
        orderWallet.setOrderNo(orderNo);
        orderWallet.setOrderUserId(userId);
        orderWallet.setActualAmount(wallet.getWalletPrice());
        orderWallet.setInitAmount(wallet.getWalletValue());

        if(orderWallet.getPayType().equals(UserConstants.PAY_TYPE_WX_OFFICIAL_ACCOUNT)){
            // 使用微信公众号支付
            CustomerUser cu = customerUserService.selectUserById(userId);
            BigDecimal yibai = new BigDecimal("100");
            float actualAmount = wallet.getWalletPrice().multiply(yibai).intValue();
            // totalFee 必须要以分为单位
            Object createOrderResult = bizWechatPayService.createOrder("钱包充值", orderNo, (int) actualAmount,
                    cu.getOpenId(), UserConstants.PAY_TYPE_WX_OFFICIAL_ACCOUNT, UserConstants.FINANCE_RECHARGE, orderWallet.getShopId());
            orderWallet.setPayParams(createOrderResult);
        }

        orderWalletMapper.insertOrderWallet(orderWallet);

        return orderWallet;
    }

    /**
     * 修改保存充值订单信息
     * 
     * @param orderWallet 充值订单信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateOrderWallet(OrderWallet orderWallet)
    {
        return orderWalletMapper.updateOrderWallet(orderWallet);
    }

    /**
     * 修改充值订单支付状态
     *
     * @param orderWallet 充值订单信息
     * @return 结果
     */
    @Override
    public int updateOrderWalletPayStatus(OrderWallet orderWallet)
    {
        return orderWalletMapper.updateOrderWallet(orderWallet);
    }

    // 结算中心
    @Override
    public void settleCenter(OrderWallet orderWallet){
        orderWallet.setPayStatus(UserConstants.PAY_STATUS_PAID);
        // 订单更改
        orderWalletMapper.updateOrderWallet(orderWallet);

        // 客户财务记录
        customerFinanceCenter(orderWallet, UserConstants.FINANCE_RECHARGE);
        // 客户数据统计
        customerStaticService.updateUserRecharge(orderWallet.getOrderUserId(), orderWallet.getInitAmount());

        // 更新客户余额
        CustomerUser customerUser = customerUserService.selectUserById(orderWallet.getOrderUserId());
        customerUser.setBalance(customerUser.getBalance().add(orderWallet.getInitAmount()));
        customerUserService.updateUserMoney(customerUser);
    }

    // 客户财务记录
    public void customerFinanceCenter(OrderWallet orderWallet, String financeType){
        CustomerFinance customerFinance = new CustomerFinance();
        customerFinance.setUserId(orderWallet.getOrderUserId());
        customerFinance.setFinanceType(financeType);
        customerFinance.setOrderNo(orderWallet.getOrderNo());
        customerFinance.setAmount(orderWallet.getActualAmount());
        customerFinance.setShopId(orderWallet.getShopId());
        customerFinance.setRemark("到账：" + orderWallet.getInitAmount());
        customerFinanceService.insertCustomerFinance(customerFinance);
    }

    /**
     * 通过充值订单编号删除充值订单
     * 
     * @param orderNo 充值订单编号
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteOrderWalletByOrderNo(String orderNo)
    {
        return orderWalletMapper.deleteOrderWalletByOrderNo(orderNo);
    }

    /**
     * 批量删除充值订单信息
     * 
     * @param orderNos 需要删除的充值订单编号
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteOrderWalletByOrderNos(String[] orderNos)
    {
        return orderWalletMapper.deleteOrderWalletByOrderNos(orderNos);
    }
}
